package com.sfpay.coreplatform.account.service.impl;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.sfpay.coreplatform.account.common.AccountResponseCode;
import com.sfpay.coreplatform.account.persistence.dao.IAccountDao;
import com.sfpay.coreplatform.account.service.IAccountPasswordMgrService;
import com.sfpay.coreplatform.account.service.exception.AccountServiceException;
import com.sfpay.coreplatform.account.valueobject.dto.ResetAccountPsw;
import com.sfpay.coreplatform.account.valueobject.dto.UpdateAccountPsw;
import com.sfpay.coreplatform.account.valueobject.tmo.AccountVO;
import com.sfpay.framework.common.util.AssertUtils;

@Service
public class AccountPasswordMgrServiceImpl implements
		IAccountPasswordMgrService {
	
	private static Logger logger = LoggerFactory.getLogger(AccountPasswordMgrServiceImpl.class);
	
	@Resource
	private IAccountDao accountDao;
	
	@Override
	@Transactional(rollbackFor=AccountServiceException.class)
	public void updateAccountPsw(UpdateAccountPsw updateAccountPsw)
			throws AccountServiceException {
		logger.debug("updateAccountPsw(UpdateAccountPsw) - start"); //$NON-NLS-1$

		try{
			AssertUtils.notNull(updateAccountPsw);
			AssertUtils.notNullAndEmpty(updateAccountPsw.getAccountNo());
			AssertUtils.notNullAndEmpty(updateAccountPsw.getAccountPasswdNew());
			AssertUtils.notNullAndEmpty(updateAccountPsw.getAccountPasswdOld());

			AccountVO accountVO = accountDao.selectByAccountNo(updateAccountPsw.getAccountNo());
			if(updateAccountPsw.getAccountPasswdOld().equals(accountVO.getAccountPasswd())){
				AccountVO accountTmp = new AccountVO();
				accountTmp.setAccountNo(updateAccountPsw.getAccountNo());
				accountTmp.setAccountPasswd(updateAccountPsw.getAccountPasswdNew());

				AssertUtils.notUpdateMoreThanOne(accountDao.updatePswByAccountNo(accountTmp));
			}else{
				throw new AccountServiceException(AccountResponseCode.ACCOUNT_PSW_ERROR,"The account password is wrong");
			}

		}catch (AccountServiceException e) {
			throw e;
		} catch (Exception e) {
			logger.error("update account password wrong",e);
			throw new AccountServiceException(AccountResponseCode.ACCOUNT_UPDATE_PSW_ERROR,"update account password wrong",e);
		}

		logger.debug("updateAccountPsw(UpdateAccountPsw) - end"); 
	}

	
	@Override
	@Transactional(rollbackFor=AccountServiceException.class)
	public void resetAccountPsw(ResetAccountPsw resetAccountPsw)
			throws AccountServiceException {
		logger.debug("resetAccountPsw(ResetAccountPsw resetAccountPsw=" + resetAccountPsw + ") - start"); //$NON-NLS-1$ //$NON-NLS-2$

		try{
			AssertUtils.notNull(resetAccountPsw);
			AssertUtils.notNullAndEmpty(resetAccountPsw.getAccountNo());
			AssertUtils.notNullAndEmpty(resetAccountPsw.getAccountPasswdNew());

			AccountVO accountTmp = new AccountVO();
			accountTmp.setAccountNo(resetAccountPsw.getAccountNo());
			accountTmp.setAccountPasswd(resetAccountPsw.getAccountPasswdNew());

			AssertUtils.notUpdateMoreThanOne(accountDao.updatePswByAccountNo(accountTmp));

		}catch (Exception e) {
			logger.error("reset account password wrong",e);
			throw new AccountServiceException(AccountResponseCode.ACCOUNT_RESET_PSW_ERROR,"reset account password wrong",e);
		}

		logger.debug("resetAccountPsw(ResetAccountPsw) - end");
	}

	

}
